﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.Caching;

namespace BasicCMS.Caching.Impl
{
    /// <summary>
    /// 基于Asp.net的缓存提供者
    /// </summary>
    [ComponentRegistry(Lifetime.Singleton,"cache","aspnet",IsDefault=true)]
    public class AspNetCacheProvider : ICacheProvider
    {
        private Cache Cache
        {
            get { return HttpContext.Current.Cache ?? HttpRuntime.Cache; }
        }

        public T Get<T>(string key)
        {
            return (T)Cache.Get(key);
        }

        public void Insert(string key, object instance)
        {
            Cache.Insert(key, instance, null, DateTime.MaxValue, TimeSpan.Zero, CacheItemPriority.Normal, null);
        }

        public void Insert(string key, object instance, double seconds)
        {
            Cache.Insert(key, instance, null, DateTime.Now.AddSeconds(seconds), TimeSpan.Zero, CacheItemPriority.Normal, null);
        }

        public void Remove(string key)
        {
            Cache.Remove(key);
        }
    }
}
